package br.unicamp.mc747.grupo12;

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

import br.unicamp.mc747.grupo12.dto.Cliente;
import br.unicamp.mc747.grupo12.dto.Result;

import com.thoughtworks.xstream.XStream;

public class ConsultaCliente {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		try {

			// Testa a consulta de cliente por cpf/cnpj
			Result rt1 = consultarCliente("35078105492");
			System.out.println("consultarCliente('consultarCliente'): " + rt1);

			// Testa a consulta de cliente com um filtro especifico
			Result rt2 = buscarCliente(10, 1, "False", "grupo", "", "", "", "");
			System.out
					.println("buscarCliente('10', '1', 'False', 'grupo', '', '', '', ''): "
							+ rt2);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * Realiza a pesquisa de cliente por cpf ou cnpj
	 * 
	 * @param cpfCnpj
	 * @return
	 * @throws MalformedURLException
	 * @throws XmlRpcException
	 */
	private static Result consultarCliente(String cpfCnpj)
			throws MalformedURLException, XmlRpcException {
		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
		config.setServerURL(new URL("http://201.77.177.204:8082"));
		XmlRpcClient client = new XmlRpcClient();
		client.setConfig(config);
		Object[] params = new Object[] { cpfCnpj };
		String result = (String) client.execute("consultarCliente", params);

		System.out.println("Result: " + result);

		XStream xstrem = new XStream();
		xstrem.alias("result", Result.class);
		xstrem.alias("cliente", Cliente.class);

		result = result.replaceAll("\n", "");
		result = result.replaceAll("\t", "");

		Result rt = (Result) xstrem.fromXML(result);
		return rt;
	}

	/**
	 * Realiza a pesquisa de cliente por filtro.
	 * 
	 * @param qtdRegistroPagina
	 * @param pagina
	 * @param composicaoAnd
	 * @param nome
	 * @param dataNascimento
	 * @param cidade
	 * @param estado
	 * @param email
	 * @return
	 * @throws MalformedURLException
	 * @throws XmlRpcException
	 */
	private static Result buscarCliente(int qtdRegistroPagina, int pagina,
			String composicaoAnd, String nome, String dataNascimento,
			String cidade, String estado, String email)
			throws MalformedURLException, XmlRpcException {
		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
		config.setServerURL(new URL("http://201.77.177.204:8082"));
		XmlRpcClient client = new XmlRpcClient();
		client.setConfig(config);
		Object[] params = new Object[] { qtdRegistroPagina, pagina,
				composicaoAnd, nome, dataNascimento, cidade, estado, email };
		String result = (String) client.execute("buscarCliente", params);

		System.out.println("Result: " + result);

		XStream xstrem = new XStream();
		xstrem.alias("result", Result.class);
		xstrem.alias("cliente", Cliente.class);

		result = result.replaceAll("\n", "");
		result = result.replaceAll("\t", "");

		Result rt = (Result) xstrem.fromXML(result);
		return rt;
	}

}
